package com.tanovo.api;

import com.tanovo.bo.*;
import com.tanovo.domain.Company;
import com.tanovo.service.ICompanyService;
import com.tanovo.service.IOauthClientTokenService;
import com.tanovo.service.ITokenService;
import com.tanovo.service.IUserService;
import com.tanovo.util.ValidateCodeUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Api(tags = "登录认证")
public class AuthApi {

    @Autowired
    private ITokenService iTokenService;
    @Autowired
    private IUserService iUserService;
    @Autowired
    private ICompanyService iCompanyService;
    @Autowired
    private IOauthClientTokenService oauthClientTokenService;



    @ApiOperation(value = "获取token", notes = "用户名密码获取token对象")
    @PostMapping("/oauth/token")
    public ResultObject<Token> getToken(@RequestBody @Validated TokenRequest tokenRequest) {
        Token token = iTokenService.getUserToken(tokenRequest);
        ResultObject result = new ResultObject(token);
        return result;
    }

    @ApiOperation(value = "（前端）判断token是不是超时或者失效", notes = "（前端）判断token是不是超时或者失效")
    @GetMapping("/oauth/checkToken")
    public ResultObject<Boolean> checkToken(@RequestParam String token) {
        Boolean flag = iTokenService.checkToken(token);
        ResultObject result = new ResultObject(flag);
        return result;
    }


    @ApiOperation(value = "注册安全平台账号", notes = "注册安全平台账号")
    @PostMapping("/oauth/register")
    public ResultObject register(@RequestBody UserRegisterBo userRegisterBo) {
        iUserService.register(userRegisterBo);
        ResultObject result = new ResultObject();
        return result;
    }

    @ApiOperation(value = "用户能登录的公司列表", notes = "用户能登录的公司列表")
    @GetMapping("/oauth/pass/companyList")
    public ResultObject<List<Company>> companyPassList(@RequestParam Integer userId) {
        List<Company> list = iCompanyService.userLoginCompanyList(userId);
        ResultObject result = new ResultObject(list);
        return result;
    }

    @ApiOperation(value = "用户需要注册公司列表", notes = "用户需要注册公司列表")
    @GetMapping("/oauth/register/companyList")
    public ResultObject<List<Company>> companyRegisterList() {
        CompanyQueryBo queryBo = new CompanyQueryBo();
        List<Company> list = iCompanyService.list(queryBo);
        ResultObject result = new ResultObject(list);
        return result;
    }

    @ApiOperation(value = "用户注册发送手机验证码", notes = "用户注册发送手机验证码")
    @GetMapping("/oauth/register/sendMsg")
    public ResultObject sendMsgByPhone(@RequestParam String phone,@RequestParam String imageCode) {
        iUserService.sendRegisterCode(phone,imageCode);
        ResultObject result = new ResultObject();
        return result;
    }
    @ApiOperation(value = "手机号码生成图片验证码", notes = "手机号码生成图片验证码")
    @PostMapping("/oauth/getImageCodeByPhone")
    public ResultObject getImageCodeByPhone(@RequestParam String phone) {
        ValidateCodeUtil.Validate validate= iUserService.getImageCodeByPhone(phone);
        ResultObject result = new ResultObject(validate);
        return result;
    }

    @ApiOperation(value = "微信扫码登录回调", notes = "微信扫码登录回调")
    @GetMapping("/oauth/wechat/callback")
    public ResultObject callback(@RequestParam String code, @RequestParam String clientId) {
        Token token = iTokenService.getWechatToken(code, clientId);
        ResultObject result = new ResultObject(token);
        return result;
    }

    @ApiOperation(value = "刷新数据传参", notes = "刷新数据传参")
    @PostMapping("/client/switch/dataInfo")
    public ResultObject swithcDataInfo(@RequestBody SwithcDataInfoBo swithcDataInfoBo) {
        oauthClientTokenService.swithcDataInfo(swithcDataInfoBo);
        ResultObject result = new ResultObject();
        return result;
    }
}
